﻿namespace Ops.Host.Core.Services.Impl;

internal sealed class DvEnergyConsumeService : IDvEnergyConsumeService
{
    private readonly SqlSugarRepository<DvEnergyConsume> _energyConsumeRepo;

    public DvEnergyConsumeService(SqlSugarRepository<DvEnergyConsume> energyConsumeRepo)
    {
        _energyConsumeRepo = energyConsumeRepo;
    }

    public async Task AddEnergyConsumeAsync(DvEnergyConsume data)
    {
        await _energyConsumeRepo.InsertAsync(data);
    }

    public async Task<PagedList<DvEnergyConsume>> GetPagedListAsync(EnergyConsumeFilter filter, int pageIndex, int pageSize)
    {
        return await _energyConsumeRepo.AsQueryable()
                .WhereIF(!string.IsNullOrWhiteSpace(filter.Category), s => s.Category == filter.Category)
                .WhereIF(filter.CreateTimeStart != null, s => s.CreateTime >= filter.CreateTimeStart.ToDayMin())
                .WhereIF(filter.CreateTimeEnd != null, s => s.CreateTime <= filter.CreateTimeEnd.ToDayMax())
                .OrderByDescending(s => s.CreateTime)
                .ToPagedListAsync(pageIndex, pageSize);
    }
}
